package codename.carrot.bomberman;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * A class that provides loads and saves (read/write) to player stats
 * @author Group A4
 *
 */
public class StatsPreferences {
	
	/**
	 * The name of preferenses for games lost.
	 */
    private static final String GAMES_LOST = "games_lost";
    /**
     * The name of prferenses for games won.
     */
    private static final String GAMES_WON = "games_won";
    /**
     * The name of preferenses for bombs dropped.
     */
    private static final String BOMBS_DROPPED = "bombs_dropped";
    /**
     * The name of preferenses for items collected.
     */
    private static final String ITEMS_COLLECTED = "items_collected";
    /**
     * The name of preferenses for lives lost.
     */
    private static final String LIVES_LOST = "lifes_lost";
    /**
     * The name of preferenses for lives taken.
     */
    private static final String LIVES_TAKEN = "players_killed";
    
    /**
     * THe name of preferences for self lives taken.
     */
    private static final String SELF_LIVES_TAKEN = "self_killed";
    /**
     * The shared preferenses used for saving and loading states.
     */
    private SharedPreferences prefs;
    
    /**
     * Constructs the stat preferenses.
     * @param prefs The preferenses to save/load from.
     */
    public StatsPreferences(SharedPreferences prefs) {
        this.prefs = prefs;
    }
       
    /**
     * Updates and adds 1 to the count of games lost
     */
    public void addGameLost() {
        if(prefs == null) return;
        Editor edit = prefs.edit();
        edit.putInt(GAMES_LOST, prefs.getInt(GAMES_LOST, 0)+1);
        edit.commit();
    }
    
    /**
     *  Updates and adds 1 to the count of games won
     */
    public void addGameWon() {
        if(prefs == null) return;
        Editor edit = prefs.edit();
        edit.putInt(GAMES_WON, prefs.getInt(GAMES_WON,0)+1);
        edit.commit();
    }
    
    /**
     *  Updates and adds 1 to the count of items collected
     */
    public void addItemsCollected() {
        if(prefs == null) return;
        Editor edit = prefs.edit();
        edit.putInt(ITEMS_COLLECTED, prefs.getInt(ITEMS_COLLECTED,0)+1);
        edit.commit();
    }
    
    /**
     *  Updates and adds <code>bombs</code> to the count of bombs dropped
     * @param bombs nr of bombs dropped
     */
    public void addBombsDropped(int bombs) {
        if(prefs == null) return;
        Editor edit = prefs.edit();
        edit.putInt(BOMBS_DROPPED, prefs.getInt(BOMBS_DROPPED,0)+bombs);        
        edit.commit();
    }
    
    /**
     *  Updates and adds <code>lives</code> to the count of lives lost
     * @param lives number of lives lost
     */
    public void addLifeLost(int lives) {
        if(prefs == null) return;
        Editor edit = prefs.edit();
        edit.putInt(LIVES_LOST, prefs.getInt(LIVES_LOST,0)+lives);
        edit.commit();
    }
    
    /**
     *  Updates and adds <code>players</code> to the count of lives taken
     * @param lives
     */
    public void addLifeTaken(int lives) {
        if(prefs == null) return;
        Editor edit = prefs.edit();
        edit.putInt(LIVES_TAKEN, prefs.getInt(LIVES_TAKEN, 0)+lives);
        edit.commit();
    }
    
    public void addSelfLifeTaken(int lives) {
    	if(prefs == null) return;
    	Editor edit = prefs.edit();
    	edit.putInt(SELF_LIVES_TAKEN, prefs.getInt(SELF_LIVES_TAKEN, 0)+lives);
    	edit.commit();
    }
    
    /**
     *  Returns the number of lives taken
     * @return lives taken
     */
    public int getNrOfLivesTaken() {
        if(prefs == null) return 0;
        return prefs.getInt(LIVES_TAKEN, 0);        
    }
    /**
     *  Returns the number of times the player hurt himself
     * @return self lives taken
     */
    public int getNrOfSelfLivesTaken() {
    	if(prefs == null) return 0;
    	return prefs.getInt(SELF_LIVES_TAKEN, 0);
    }
    /**
     *  Returns the number of lives lost
     * @return lives lost
     */
    public int getNrOfLivesLost() {
        if(prefs == null) return 0;
        return prefs.getInt(LIVES_LOST, 0);
    }
    
    /**
     *  Returns the number of items collected
     * @return items collected
     */
    public int getNrOfCollectedItems() {
        if(prefs == null) return 0;
        return prefs.getInt(ITEMS_COLLECTED, 0);
    }
    
    /**
     *  Returns the number of bombs dropped
     * @return bombs dropped
     */
    public int getNrOfBombsDropped() {
        if(prefs == null) return 0;
        return prefs.getInt(BOMBS_DROPPED, 0);
    }
    
    /**
     *  Returns the number of games lost
     * @return games lost
     */
    public int getNrOfGamesLost() {
        if(prefs == null) return 0;
        return prefs.getInt(GAMES_LOST, 0);
    }
    
    /**
     *  Returns the number of games won
     * @return games won
     */
    public int getNrOfGamesWon() {
        if(prefs == null) return 0;
        return prefs.getInt(GAMES_WON, 0);
    }
    
    
}
